﻿using Admin.ApplicationLib;
using Admin.ApplicationLib.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Admin.WebApi.Infrastructure.Store
{
    public class CustomUserStore : UserStore<UserEntity, RoleEntity, AdminApplicationDbContext, Guid>
    {
        private readonly AdminApplicationDbContext _context;

        public CustomUserStore(AdminApplicationDbContext context, IdentityErrorDescriber? describer = null) : base(context,
            describer)
        {
            _context = context;
        }

        public override async Task<IList<string>> GetRolesAsync(UserEntity user,
            CancellationToken cancellationToken = new CancellationToken())
        {
            return await (from userRole in _context.UserRoles
                join role in _context.Roles on userRole.RoleId equals role.Id
                where userRole.UserId == user.Id
                select role.Code).ToListAsync(cancellationToken: cancellationToken);
        }
    }
}